package com.huike.web.controller.review;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.apache.ibatis.annotations.Delete;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save1(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex){

        Review review = new Review();
        review.setAge(age);
        review.setName(name);
        review.setSex(sex);
        review.setCreateBy("1");
        review.setCreateTime(new Date());
        review.setUpdateBy("1");
        review.setUpdateTime(new Date());
        reviewService.save1(review);
        return new AjaxResult(200,"成功插入:1条数据" );

    }

    @PostMapping("/saveData")
    public AjaxResult save2(@RequestBody Review review){

        review.setCreateBy("1");
        review.setCreateTime(new Date());
        review.setUpdateBy("1");
        review.setUpdateTime(new Date());
        reviewService.save2(review);
        return new AjaxResult(200,"成功插入:1条数据" );
    }

    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){

        reviewService.delete(id);

        return new AjaxResult(200,"成功删除:1条数据" );

    }

    /**=========================================================修改数据=============================================*/

    @PutMapping("/update")
    public AjaxResult updateById(@RequestBody Review review){
        reviewService.upDate(review);
        return new AjaxResult(200,"修改成功" );
    }
    /**=========================================================查询数据=============================================*/
    @GetMapping("/getById")
    public AjaxResult selectById(@RequestParam Integer id){

        Review review = reviewService.selectById(id);

        return new AjaxResult(200, "操作成功", review);

    }

    @PostMapping("/getDataByPage/{pageNum}/{pageSize}")
    public AjaxResult selectByPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@RequestBody Review review){

        PageHelper.startPage(pageNum, pageNum);
        List<Review> reviews = reviewService.selectByPage(review);
        PageInfo<Review> pageInfo = new PageInfo<>(reviews);

        return new AjaxResult(200, "查询成功", pageInfo);
    }

}